﻿using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Threading;
using System.Windows.Data;

namespace Converter
{
    public class PercentConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// This method is used to bind the silverlight component to the resource. 
        /// </summary>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || value.ToString() =="")
                return "";  
            return String.Format("{0:F2}", ((double)value) * 100) + "%";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || value.ToString() == "")
                return null;
            try
            {
                double percent = (double)Double.Parse(value.ToString().Split('%')[0].ToString().Replace('.',','));
                return percent / 100;
                //return float.Parse( String.Format("{0:F2}", percent / 100));
            }
            catch 
            {
                return null;
            }
        }

        #endregion
    }
}